/*
 * Copyright (C) 2018 Zhejiang xiaominfo Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.xiaominfo.com.
 * Developer Web Site: http://open.xiaominfo.com.
 */

package com.github.xiaoymin.map;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil;
import com.github.xiaoymin.map.common.MapClientType;
import com.github.xiaoymin.map.exception.MapClientException;
import com.github.xiaoymin.map.model.AmapKey;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 外部配置类
 * @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a>
 * 2020/09/21 13:20
 * @since:map-client 1.0
 */
public class MapClientConfig {
    /**
     * 高德地图keys
     */
    private List<AmapKey> amapKeys=new ArrayList<>();
    /**
     * 百度地图keys
     */
    private List<String> bmapKeys=new ArrayList<>();
    /**
     * 腾讯地图keys
     */
    private List<String> tencentmapKeys=new ArrayList<>();

    /**
     * 配置高德地图的开发秘钥(该配置默认不需要签名)
     * @param keys 开发秘钥
     * @return client配置
     */
    public MapClientConfig keysAmap(String...keys){
        if (ArrayUtil.isEmpty(keys)){
            throw new MapClientException("高德地图开发者秘钥不能为空");
        }
        for (String key:keys){
            amapKeys.add(new AmapKey(key,false,null));
        }
        return this;
    }

    /**
     * 配置高德地图的开发秘钥(开发者根据默认keys自己定义是否开启签名配置)
     * @param mapKeys  开发秘钥
     * @return client配置
     */
    public MapClientConfig keysAmap(AmapKey...mapKeys){
        if (ArrayUtil.isEmpty(mapKeys)){
            throw new MapClientException("高德地图开发者秘钥不能为空");
        }
        amapKeys.addAll(Arrays.asList(mapKeys));
        return this;
    }

    /**
     * 配置百度地图开发秘钥
     * @param keys 开发秘钥
     * @return client配置
     */
    public MapClientConfig keysBaiduMap(String...keys){
        if (ArrayUtil.isEmpty(keys)){
            throw new MapClientException("百度地图开发者秘钥不能为空");
        }
        bmapKeys.addAll(Arrays.asList(keys));
        return this;
    }

    /**
     * 配置腾讯地图的开发秘钥
     * @param keys 开发秘钥
     * @return client配置
     */
    public MapClientConfig keysTencentMap(String...keys){
        if (ArrayUtil.isEmpty(keys)){
            throw new MapClientException("腾讯地图开发者秘钥不能为空");
        }
        tencentmapKeys.addAll(Arrays.asList(keys));
        return this;
    }

    /**
     * 获取腾讯地图的开发秘钥配置
     * @return 秘钥配置
     */
    public String getTenCentKey(){
        if (CollectionUtil.isEmpty(tencentmapKeys)){
            throw new MapClientException("暂时没有配置腾讯地图开发者秘钥Key");
        }
        //随机获取一个开发者秘钥
        return tencentmapKeys.get(RandomUtil.randomInt(0,tencentmapKeys.size()));
    }

    /**
     * 获取腾讯地图的开发秘钥配置
     * @return 秘钥配置
     */
    public String getBaiduKey(){
        if (CollectionUtil.isEmpty(bmapKeys)){
            throw new MapClientException("暂时没有配置百度地图开发者秘钥Key");
        }
        //随机获取一个开发者秘钥
        return bmapKeys.get(RandomUtil.randomInt(0,bmapKeys.size()));
    }



    /**
     * 获取高德地图的开发秘钥配置
     * @return 秘钥配置
     */
    public AmapKey getAmapKey(){
        if (CollectionUtil.isEmpty(amapKeys)){
            throw new MapClientException("暂时没有配置高德地图开发者秘钥Key");
        }
        //随机获取一个开发者秘钥
        return amapKeys.get(RandomUtil.randomInt(0,amapKeys.size()));
    }

    /**
     * 根据地图类型获取开发秘钥
     * @param mapClientType 地图类型
     * @return 开发秘钥
     */
    public String getMapKey(MapClientType mapClientType){
        String key=null;
        switch (mapClientType){
            case BAIDUMAP:
                key=checkKeys(bmapKeys,"暂时没有配置百度地图开发者秘钥Key");
                break;
            case TENCENTMAP:
                key=checkKeys(tencentmapKeys,"暂时没有配置腾讯地图开发者秘钥Key");
                break;
            default:
                throw new MapClientException("地图类型:"+mapClientType+"暂不支持");
        }
        return key;
    }

    /**
     * 取开发者秘钥前校验集合是否为空
     * @param keys 开发者秘钥集合
     * @param err 错误信息
     */
    private String checkKeys(List<String> keys,String err){
        if (CollectionUtil.isEmpty(keys)){
            throw new MapClientException(err);
        }
        //随机获取一个开发者秘钥
        return keys.get(RandomUtil.randomInt(0,keys.size()));
    }

}
